package com.sevenstudio.adu.gasing.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.ui.IGameInterface;

import android.view.KeyEvent;

import com.sevenstudio.adu.gasing.bluetooth.BT_Server_Client;
import com.sevenstudio.adu.gasing.define.GameEngineConfiguration;
import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.element.ABackground;
import com.sevenstudio.adu.gasing.element.AScene;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;



public class State_Select_Mode 
{
	GameEngine engine;
	AScene scene;
	
	ABackground bg_mode;
	ASprite[] mode = new ASprite[3];
	
	public State_Select_Mode(GameEngine engine)
	{
		this.engine = engine;
		scene = new AScene(0);
		
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
		Load();
	}

	private void Load() 
	{
		bg_mode = new ABackground(engine, iGameDefines.SPRITE_SELECT_MODE, iGameDefines.INDEX_BG_MODE);
		
		for(int i=0; i < 3; i++)
		{
			mode[i] = new ASprite(engine, iGameDefines.SPRITE_SELECT_MODE, 1 + i);
			scene.theScene().registerTouchArea(mode[i].theSprite());
			Utils.TRACE("ini "+i);
		}
		
	}

	public void Init() 
	{
		scene.theScene().detachChildren();
		
		engine.SetGameScene(scene);
		engine.SceneAttach(scene, bg_mode);
		
		float padding = Utils.GetRatioWidth(10);
		
		mode[1].SetPos((GameEngine.CAMERA_WIDTH - mode[1].theSprite().getWidth()) / 2, mode[1].GetY());
		mode[0].SetPos(mode[1].GetX() - (mode[0].theSprite().getWidth() + padding) , mode[0].GetY());
		mode[2].SetPos(mode[1].GetX() +  mode[1].theSprite().getWidth() + padding, mode[0].GetY());
		
		for(int i=0; i < 3; i++)
		{
			
			engine.SceneAttach(scene, mode[i]);
			
		}
		
		engine.server_Client.stopBTThread();
	}

	public void Update() 
	{
		
		
	}

	public void Exit() 
	{
	
		final EngineLock lock = engine.getEngine().getEngineLock();
		
		lock.lock();
		bg_mode.DetachSelf();
		for(int i=0; i < 3; i++)
		{
			mode[i].DetachSelf();
		}
		
		lock.unlock();
		
	}

	
		
	public void keyDown(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			Exit();
			engine.SetNextMessageState(iGameDefines.MESSAGE_STATE_INIT);
			engine.SetNextGameState(iGameDefines.GAME_STATE_MAINMENU);
		}
	}

	public void Touch(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if(pSceneTouchEvent.isActionUp())
		{
		}
		if(pSceneTouchEvent.isActionUp() && pTouchArea.equals(mode[0].theSprite()))
		{
			GameEngineConfiguration.ModeSelected= iGameDefines.MODE_SINGLE;
			engine.SetNextMessageState(engine.NextMessageState());

			BT_Server_Client.isBluetooth = false;
//			engine.SetNextMessageState(iGameDefines.MESSAGE_STATE_INIT);
//			engine.SetNextGameState(iGameDefines.GAME_STATE_SELECT_PLAYER);
		}
		else if(pSceneTouchEvent.isActionUp() && pTouchArea.equals(mode[1].theSprite()))
		{
			GameEngineConfiguration.ModeSelected= iGameDefines.MODE_MULTI;
			GameEngine.context.runOnUiThread(new Runnable()
			{
				public void run()
				{
					engine.server_Client.modeOn();
				}
			});
		}
		else if(pSceneTouchEvent.isActionUp() && pTouchArea.equals(mode[2].theSprite()))
		{
			State_Main_Menu.bgm_menu.Stop();
			GameEngineConfiguration.ModeSelected= iGameDefines.MODE_CAREER;
			engine.SetNextMessageState(engine.NextMessageState());
		}
			 
	}

	public void onClick(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY) {
		// TODO Auto-generated method stub
		
	}
	
	
}
